from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.dialects.mysql import LONGTEXT
from auth.db import Base
from datetime import datetime, timedelta, timezone
# 东八区时区对象（中国标准时间）
CST = timezone(timedelta(hours=8))
def now_cst():
    """返回当前北京时间（带时区信息）"""
    return datetime.now(CST)

class ECGRecord(Base):
    __tablename__ = "ecg_records"
    # Ensure table uses utf8mb4 for Chinese support
    __table_args__ = {'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_unicode_ci'}

    id = Column(Integer, primary_key=True, index=True)
    device_id = Column(String(64), ForeignKey("devices.device_id", ondelete="CASCADE", onupdate="CASCADE"), index=True)
    owner = Column(String(64), ForeignKey("users.username", ondelete="CASCADE", onupdate="CASCADE"), index=True)
    start_time = Column(DateTime(timezone=True), default=now_cst)
    end_time = Column(DateTime(timezone=True), default=now_cst, onupdate=now_cst, nullable=True)
    data = Column(LONGTEXT)
    timestamps = Column(LONGTEXT, nullable=True)

